// $codepro.audit.disable
/* Copyright (c) 2011 Team A CS2340. All rights reserved.*/

package edu.gatech.teama.doctorsoffice.client.view;


import javax.swing.JPanel;
import java.awt.GridBagLayout;
import javax.swing.JButton;

import org.joda.time.LocalDate;
import org.joda.time.LocalDateTime;

import com.thirdnf.ResourceScheduler.Scheduler;
import com.toedter.calendar.JDateChooser;

import edu.gatech.teama.doctorsoffice.client.presenter.AppointmentLookupPresenter;
import edu.gatech.teama.doctorsoffice.client.presenter.ServerConnection;
import edu.gatech.teama.doctorsoffice.client.view.scheduler.AppointmentRequestSchedulerModel;
import edu.gatech.teama.doctorsoffice.model.Appointment;
import edu.gatech.teama.doctorsoffice.model.User;

import java.awt.GridBagConstraints;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.awt.Insets;
import javax.swing.JComboBox;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import net.miginfocom.swing.MigLayout;

/**
 * AppointmentLookupPanel.java
 * @author unknown
 * @version 1.0
 */
public class AppointmentLookupPanel extends JPanel {

	private AppointmentLookupPresenter myPresenter;

	private List<Appointment> appointments;

	/**
	 * Create the panel.
	 */
	public AppointmentLookupPanel() { // $codepro.audit.disable com.instantiations.assist.eclipse.analysis.audit.rule.effectivejava.constructorsOnlyInvokeFinalMethods
		myPresenter = new AppointmentLookupPresenter();
		final ServerConnection sc = ServerConnection.getInstance();
		setLayout(new MigLayout("", "[230px][65px]", "[20px][20px][23px]"));

		JButton btnModify = new JButton("Modify");
		add(btnModify, "cell 1 2,alignx center,aligny center");

		final JComboBox comboBox = new JComboBox();

		final JDateChooser dateChooser = new JDateChooser(new Date());
		dateChooser.addPropertyChangeListener(new PropertyChangeListener() {
			public void propertyChange(PropertyChangeEvent evt) {
				if(ServerConnection.getInstance().getLoggedInUser()!=null) {
					appointments = myPresenter.getAppointments(new LocalDateTime(dateChooser.getDate()));
					comboBox.removeAllItems();
					for (Appointment a : appointments) {
						User u = sc.getUser(a.getPatientUsername());

						String name = u.getFirstName() + " " + u.getLastName();
						comboBox.addItem(a.getDate().toString("HH:mm") + " - " + name);

					}
				}
			}
		});
		btnModify.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (comboBox.getSelectedIndex() >= 0) {
					Appointment selected = appointments.get(comboBox.getSelectedIndex());

					myPresenter.openAppointment(selected);
				}
			}
		});
		final Scheduler scheduler = new Scheduler();
		scheduler.setModel(new AppointmentRequestSchedulerModel());
		scheduler.showDate(new LocalDate(dateChooser.getDate()));
		add(dateChooser, "cell 0 0,growx,aligny center");
		add(comboBox, "cell 0 1,growx,aligny center");



	}
}
